package com.petRescueCenter.demo.posting.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.IterableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.petRescueCenter.demo.common.enumPackage.LockedEmun;
import com.petRescueCenter.demo.common.web.VuejsPageRequest;
import com.petRescueCenter.demo.posting.dao.PostingDao;
import com.petRescueCenter.demo.posting.entity.Posting;
import com.petRescueCenter.demo.posting.entity.dto.PostingDTO;
import com.petRescueCenter.demo.user.dao.UserDao;
import com.petRescueCenter.demo.user.entity.User;

@Service
public class PostingService implements IPostingService {

	@Autowired
	private PostingDao postingDao;
	@Autowired
	private UserDao userDao;

	@Override
	public Posting save(Posting entity) {
		// TODO Auto-generated method stub
		return postingDao.save(entity);
	}

	@Override
	public Posting findById(Long id) {
		// TODO Auto-generated method stub
		return postingDao.findById(id).get();
	}

	@Override
	public boolean existsById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void deleteById(Long id) {
		// TODO Auto-generated method stub
		postingDao.deleteById(id);
	}

	@Override
	public void deleteAll(Long[] ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Page<PostingDTO> findAll(Specification<Posting> spec, Pageable pageable) {
		Page<Posting> entityLists = postingDao.findAll(spec, pageable);
		List<PostingDTO> dtoLists = null;
		if (entityLists != null) {
			dtoLists = new ArrayList<PostingDTO>();
			for (Posting entity : entityLists) {

				PostingDTO dto = new PostingDTO();
				PostingDTO.entityToDto(entity, dto);

				dtoLists.add(dto);

			}
		}
		return new PageImpl<PostingDTO>(dtoLists, pageable, entityLists.getTotalElements());
	}

	@Override
	public boolean existsByName(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PostingDTO> findAll() {
		// TODO Auto-generated method stub
		Iterable<Posting> postings = postingDao.findAll();
		List<Posting> entityLists = IterableUtils.toList(postings);
		List<PostingDTO> dtoLists = null;
		if (entityLists != null) {
			dtoLists = new ArrayList<PostingDTO>();
			for (Posting entity : entityLists) {
				PostingDTO dto = new PostingDTO();
				PostingDTO.entityToDto(entity, dto);
				dtoLists.add(dto);
			}
		}
		return dtoLists;
	}

	@Override
	public Page<PostingDTO> findAll(Long fansId, List<PostingDTO> postings, Specification<Posting> spec,
			VuejsPageRequest pageRequest) {
		// TODO Auto-generated method stub
		Page<Posting> entityLists = postingDao.findAll(spec, pageRequest.getPageable());
		int totalnum = (int) entityLists.getTotalElements();
		int pagesize = pageRequest.getLimit();
		int indexpage = pageRequest.getPage();
		int star = pagesize * (indexpage - 1);
		int end = pagesize * indexpage - 1;
		List<PostingDTO> dtoLists = null;

		if (entityLists != null) {
			dtoLists = new ArrayList<PostingDTO>();
			for (Posting entity : entityLists) {
				PostingDTO dto = new PostingDTO();
				PostingDTO.entityToDto(entity, dto);
				if (fansId != null) {
					if (!entity.getFans().isEmpty()) {
						User user = userDao.findById(fansId).get();
						Set<User> users = entity.getFans();
						if (users.contains(user)) {
							dto.setAttention(LockedEmun.YES);
						} else {
							dto.setAttention(LockedEmun.NO);
						}
					}
				}
				dtoLists.add(dto);
			}
		}
		return new PageImpl<PostingDTO>(dtoLists, pageRequest.getPageable(), entityLists.getTotalElements());
	}

	@Override
	public Page<PostingDTO> findAll(Long fansId, Specification<Posting> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Posting> entityLists = postingDao.findAll(spec, pageable);
		List<PostingDTO> dtoLists = null;
		if (entityLists != null) {
			dtoLists = new ArrayList<PostingDTO>();
			for (Posting entity : entityLists) {
				PostingDTO dto = new PostingDTO();
				PostingDTO.entityToDto(entity, dto);
				if (fansId != null) {
					if (!entity.getFans().isEmpty()) {
						User user = userDao.findById(fansId).get();
						Set<User> users = entity.getFans();
						if (users.contains(user)) {
							dto.setAttention(LockedEmun.YES);
						} else {
							dto.setAttention(LockedEmun.NO);
						}
					}
				}
				dtoLists.add(dto);
			}
		}
		return new PageImpl<PostingDTO>(dtoLists, pageable, entityLists.getTotalElements());
	}

}
